home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue27 / system / UPeek1 / PackPeek.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1997-10-01  |  3.0 KB  |  122 lines

  1. unit PackPeek;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, ExtCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     OpenDialog: TOpenDialog;
  12.     CurrentFile: TLabel;
  13.     Button1: TButton;
  14.     Bevel1: TBevel;
  15.     UnitList: TListBox;
  16.     Label1: TLabel;
  17.     PackageList: TListBox;
  18.     Label2: TLabel;
  19.     procedure Button1Click(Sender: TObject);
  20.   private
  21.     { Private declarations }
  22.   public
  23.     { Public declarations }
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.DFM}
  32.  
  33. function FormatPathToFit (const fName: String; Canvas: TCanvas; AvailWidth: Integer): String;
  34. var
  35.     Idx: Integer;
  36.     Drive: String[4];
  37.     Path, Name, Ext: String;
  38.  
  39.     procedure ShortenPath;
  40.     var
  41.         StartSlash: Boolean;
  42.     begin
  43.         if Path = '\' then Path := '' else begin
  44.             if Path[1] = '\' then begin
  45.                 StartSlash := True;
  46.                 Path := Copy (Path, 2, 255);
  47.             end
  48.             else StartSlash := False;
  49.  
  50.             if Path[1] = '.' then Path := Copy (Path, 5, 255);
  51.  
  52.             Idx := Pos ('\', Path);
  53.             if Idx <> 0 then Path := '...\' + Copy (Path, Idx + 1, 255)
  54.             else Path := '';
  55.  
  56.             if StartSlash then Path := '\' + Path;
  57.         end;
  58.     end;
  59.  
  60. begin
  61.     Result := fName;
  62.     Path := ExtractFilePath (Result);
  63.     Name := ExtractFileName (Result);
  64.     Idx := Pos ('.', Name);
  65.     if Idx > 0 then SetLength (Name, Idx - 1);
  66.     Ext := ExtractFileExt (Result);
  67.     if Path [2] = ':' then begin
  68.         Drive := Copy (Path, 1, 2);
  69.         Path := Copy (Path, 3, 255);
  70.     end
  71.     else Drive := '';
  72.  
  73.     while ((Path <> '') or (Drive <> '')) and (Canvas.TextWidth (Result) > AvailWidth) do
  74.     begin
  75.         if Path = '\...\' then begin
  76.             Drive := '';
  77.             Path := '...\';
  78.         end
  79.         else if Path = '' then Drive := ''
  80.         else ShortenPath;
  81.  
  82.         Result := Drive + Path + Name + Ext;
  83.     end;
  84. end;
  85.  
  86. procedure PackageCallback (const ModuleName: string; NameType: TNameType; Flags: Byte; Param: TForm1);
  87. begin
  88.     with Param do begin
  89.         if NameType = ntContainsUnit then
  90.             UnitList.Items.Add (ModuleName)
  91.         else
  92.             PackageList.Items.Add (ModuleName);
  93.     end;
  94. end;
  95.  
  96. procedure TForm1.Button1Click (Sender: TObject);
  97. var
  98.     hLib: hModule;
  99.     PackageFlags: Integer;
  100. begin
  101.     if OpenDialog.Execute then begin
  102.         UnitList.Clear;
  103.         PackageList.Clear;
  104.         CurrentFile.Caption := FormatPathToFit (OpenDialog.FileName, Canvas, CurrentFile.Width);
  105.         hLib := LoadLibrary (PChar (OpenDialog.FileName));
  106.         if hLib <> 0 then try
  107.             { If we get here, it's a 32-bit executable }
  108.             try
  109.                 GetPackageInfo (hLib, Self, PackageFlags, @PackageCallback);
  110.             except
  111.                 { If executable has no PackageInfo resource, just bow out }
  112.                 Exit;
  113.             end;
  114.         finally
  115.             FreeLibrary (hLib);
  116.         end;
  117.     end;
  118. end;
  119.  
  120. end.
  121.  
  122.